/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>Project: app2022 - Ex6
 * <p>Powered by webrx On 2022-02-08 11:20:14
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex6 {
    volatile Vector<String> list = new Vector<>();


    public static void main(String[] args) {
        var t = new Ex6();
        for (int i = 0; i < 15; i++)
            new Thread(t::op, "T" + i).start();
    }

    void op() {
        System.out.printf("%s 启动计算中 %n", Thread.currentThread().getName());
        String[] o = new String[]{"", "+", "-"};
        Random rand = new Random();
        StringBuffer str = new StringBuffer("1");
        Pattern p = Pattern.compile("(\\d+|-\\d+)");
        while (list.size() != 11) {
            for (int i = 2; i < 10; i++)
                str.append(String.format("%s%d", o[rand.nextInt(o.length)], i));
            String s = str.toString();
            Matcher m = p.matcher(s);
            List<Integer> ln = new ArrayList<>();
            while (m.find()) {
                ln.add(Integer.parseInt(m.group()));
            }
            int sum = ln.stream().reduce(0, Integer::sum);
            if (sum == 100 && !list.contains(s)) {
                list.add(s);
                System.out.printf("[%s]:%s = 100%n", Thread.currentThread().getName(), s);
            } else {
                str.delete(1, str.length());
            }
        }
        System.out.printf("%s 结束 %n", Thread.currentThread().getName());
    }
}
